using System;
using System.IO;

namespace UnityEngine.Recorder
{
	[Serializable]
	public struct OutputPath
	{
		public enum ERoot
		{
			Absolute,
			Current,
			PersistentData,
			StreamingAssets,
			TemporaryCache,
			AssetsPath
		}

		[SerializeField]
		private ERoot m_root;

		[SerializeField]
		private string m_leaf;

		public ERoot root
		{
			get
			{
				return m_root;
			}
			set
			{
				m_root = value;
			}
		}

		public string leaf
		{
			get
			{
				return m_leaf;
			}
			set
			{
				m_leaf = value;
			}
		}

		public static OutputPath FromPath(string path)
		{
			OutputPath result = default(OutputPath);
			if (path.Contains(Application.streamingAssetsPath))
			{
				result.m_root = ERoot.StreamingAssets;
				result.m_leaf = path.Replace(Application.streamingAssetsPath, string.Empty);
			}
			else if (path.Contains(Application.dataPath))
			{
				result.m_root = ERoot.AssetsPath;
				result.m_leaf = path.Replace(Application.dataPath, string.Empty);
			}
			else if (path.Contains(Application.persistentDataPath))
			{
				result.m_root = ERoot.PersistentData;
				result.m_leaf = path.Replace(Application.persistentDataPath, string.Empty);
			}
			else if (path.Contains(Application.temporaryCachePath))
			{
				result.m_root = ERoot.TemporaryCache;
				result.m_leaf = path.Replace(Application.temporaryCachePath, string.Empty);
			}
			else if (path.Contains(Directory.GetCurrentDirectory().Replace("\\", "/")))
			{
				result.m_root = ERoot.Current;
				result.m_leaf = path.Replace(Directory.GetCurrentDirectory().Replace("\\", "/"), string.Empty);
			}
			else
			{
				result.m_root = ERoot.Absolute;
				result.m_leaf = path;
			}
			return result;
		}

		public static string GetFullPath(ERoot root, string leaf)
		{
			switch (root)
			{
			case ERoot.Absolute:
				return leaf;
			case ERoot.Current:
				return (!string.IsNullOrEmpty(leaf)) ? ("./" + leaf) : ".";
			default:
			{
				string str = string.Empty;
				switch (root)
				{
				case ERoot.PersistentData:
					str = Application.persistentDataPath;
					break;
				case ERoot.StreamingAssets:
					str = Application.streamingAssetsPath;
					break;
				case ERoot.TemporaryCache:
					str = Application.temporaryCachePath;
					break;
				case ERoot.AssetsPath:
					str = Application.dataPath;
					break;
				}
				if (!leaf.StartsWith("/"))
				{
					str += "/";
				}
				return str + leaf;
			}
			}
		}

		public string GetFullPath()
		{
			return GetFullPath(m_root, m_leaf);
		}

		public void CreateDirectory()
		{
			string fullPath = GetFullPath();
			if (fullPath.Length > 0 && !Directory.Exists(fullPath))
			{
				Directory.CreateDirectory(fullPath);
			}
		}
	}
}
